package com.ias.agdis_r.fragment.box;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BaseFragment;
import com.ias.agdis_r.activity.BaseFragmentActivity;
import com.ias.agdis_r.activity.BoxActivity;
import com.ias.agdis_r.activity.message.MessageDetailsActivity;
import com.ias.agdis_r.adapter.BoxAdapter;
import com.ias.agdis_r.manager.DataManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.terminalmanager.restful.base.endpoint.response.BaseResponse;
import com.ias.agdis_r.view.boxxlistview.MyListView;
import com.ias.agdis_r.view.boxxlistview.MyListView.IXListViewListener;
import com.ias.agdis_r.view.control.TimerProgressDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 消息盒子_短语页面
 *
 * @author guoxr
 */
public class BoxTabMessage extends BaseFragment implements IXListViewListener {
    private MyListView mListView;
    private Handler handler;
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private List<Map<String, Object>> otherslist = new ArrayList<Map<String, Object>>();
    private BoxAdapter mAdapter;
    private int loadMore = 0;
    private static final int REQUESTCODE = 3;
    public static final int RESULTCODE = 3;
    private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    private TextView noInfo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater
                .inflate(R.layout.box_tab_message, container, false);
        handler = new Handler();
        mListView = (MyListView) view.findViewById(R.id.xListView);
        noInfo = (TextView) view.findViewById(R.id.tv_noInfo);
        initData();
        mListView.setPullRefreshEnable(false);
        // 设置回调函数
        mListView.setXListViewListener(this);
        // 设置适配器
        mAdapter = new BoxAdapter(getActivity().getApplicationContext(), list);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                String tgn = list.get(position - 1).get("tgn").toString();
                if (tgn != null) {
                    Intent intent = new Intent(getActivity()
                            .getApplicationContext(),
                            MessageDetailsActivity.class);
                    intent.putExtra(MessageDetailsActivity.EXTRA_MESSAGE_TGN,
                            tgn);
                    startActivityForResult(intent, REQUESTCODE);
                }
            }
        });
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUESTCODE && resultCode == RESULTCODE) {
            BoxActivity activity = (BoxActivity) getActivity();
            activity.updateUI("ThreadMessage");
        }
    }

    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mListView.stopRefresh();
            }
        }, 1000);
    }

    @Override
    public void onLoadMore() {
        if (otherslist == null) {
            mListView.stopLoadMore();
            return;
        }
        list.addAll(addlist(otherslist));
        mAdapter.notifyDataSetChanged();
        mListView.stopLoadMore();
        if (list.size() == 0) {
            noInfo.setVisibility(View.VISIBLE);
        }
        loadMore++;
    }

    private List<Map<String, Object>> addlist(List<Map<String, Object>> other) {
        // TODO Auto-generated method stub
        List<Map<String, Object>> mylist = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < BoxActivity.LOARMORE; i++) {
            if (other.size() > 0) {
                mylist.add(other.get(0));
                other.remove(0);
            } else {
                mListView.setPullLoadEnable(false);
                break;
            }
        }
        return mylist;
    }

    private void initData() {
        // TODO Auto-generated method stub
        noInfo.setVisibility(View.GONE);
        mListView.setPullLoadEnable(true);
        if (list != null) {
            list.clear();
        }
        if (BoxActivity.messageNo != null) {
            BoxActivity.messageNo.clear();
        }
        otherslist = DataManager.getInstance().getMessageList();
        if (otherslist == null) {
            return;
        }
        for (int i = 0; i < otherslist.size(); i++) {
            if (otherslist.get(i).get("readed").equals("")) {
                list.add(otherslist.get(i));
                BoxActivity.messageNo.add(new String[]{(String) otherslist
                        .get(i).get("_id")});
                otherslist.remove(i);
                i--;
            }
        }
    }

    @Override
    protected void operateResponseSucess(String serviceId, BaseResponse response) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void updateUI(String tag) {
        // TODO Auto-generated method stub
        if (tag.equals("BoxTabMessage")) {
            initData();
            mAdapter.notifyDataSetChanged();
            // int load = loadMore;
            // for (int i = 0; i < load; i++) {
            // onLoadMore();
            // }
            // loadMore = load;
        }
    }
}
